package com.ruoyi.web.controller.h5.msg;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.H5LoginUser;
import com.ruoyi.system.vo.H5MsgDetailVO;
import com.ruoyi.system.vo.H5MsgListItemVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.ruoyi.system.dto.*;
import com.ruoyi.system.service.MsgPushService;
import com.ruoyi.system.dto.H5MsgConfirmDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * H5消息收件箱控制器
 * 提供H5用户的消息查询、确认、签署等功能
 * 支持多种消息类型的统一处理和展示
 * 
 * @author ruoyi
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/h5/msg")
public class MsgInboxController extends BaseController {

    /**
     * 消息推送服务
     */
    @Resource
    private MsgPushService pushService;

    /**
     * H5未读消息数量统计
     * 支持按消息类型筛选，用于H5端显示未读消息数量
     */
    @GetMapping("/unread-count")
    public AjaxResult unread(@Valid H5MsgUnreadCountDTO unreadCountDTO) {
        int count = pushService.getUnreadCount(unreadCountDTO);
        return AjaxResult.success(count);
    }

    /**
     * H5消息列表查询
     * 支持分页和类型过滤，返回H5用户的消息列表
     */
    @GetMapping("/list")
    public AjaxResult list(@Valid H5MsgQueryDTO queryDTO) {
        List<H5MsgListItemVO> list = pushService.getMessageList(queryDTO);
        return AjaxResult.success(list);
    }

    /**
     * H5消息详情查询
     * 根据消息类型返回对应的详细信息，包含类型化的业务数据
     */
    @GetMapping("/{itemId}")
    public AjaxResult detail(@PathVariable Long itemId) {
        H5MsgDetailVO detail = pushService.getMessageItemDetail(itemId);
        return AjaxResult.success(detail);
    }

    /**
     * H5标记消息已读
     * 标记消息为已读状态
     */
    @PostMapping("/{itemId}/read")
    public AjaxResult read(@PathVariable Long itemId) {
        boolean success = pushService.markMessageRead(itemId);
        return AjaxResult.success(success);
    }

    /**
     * H5消息统一确认接口
     * 根据消息类型执行对应的确认逻辑，如合同签署、验收确认等
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody H5MsgConfirmDTO confirmDTO) {
        return AjaxResult.success(pushService.confirmMessage(confirmDTO));
    }
}
